package com.mytest.leetcode;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Solution704 {

    public static void main(String[] args) {
        Solution704 solution704 = new Solution704();
        int[] nums = {-1,0,3,5,9,12};
        int target = 9;
        log.info("{}", solution704.search(nums, target));

        int[] nums1 = {1,3,5,6};
        int target1 = 2;
        log.info("{}", solution704.searchInsert(nums1, target1));
    }

    public int searchInsert(int[] nums, int target) {
        int result = nums.length;
        int right = nums.length -1, left = 0;
        while(left <= right){
            int mid = (right - left)/2 + left;
            if (nums[mid] == target){
                return mid;
            }else if (nums[mid] > target){
//                right = right -1;
                result = mid;
                right = mid -1;
            }else if (nums[mid] < target){
//                left = left +1;
                left = mid +1;
            }
        }
        return result;
    }


    public int search(int[] nums, int target) {
        int right = nums.length -1, left = 0;
        while(left <= right){
            int mid = (right - left)/2 + left;
            if (nums[mid] == target){
                return mid;
            }else if (nums[mid] > target){
//                right = right -1;
                right = mid -1;
            }else if (nums[mid] < target){
//                left = left +1;
                left = mid +1;
            }
        }
        return -1;
    }
}
